package unknow;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 7/16/2022 7:52 PM
 */
public class IoTest {

    private static final Logger logger = LoggerFactory.getLogger(IoTest.class);

    private static final String FILE_NAME = "../JavaTest/file/filecopy.txt";
    private static final String OUT_PUT_FILE_NAME = "../JavaTest/file/fileoutput.txt";
    private static final int BUFFER_SIZE = 1024;

    private void createFile() {
        File file1 = new File(FILE_NAME);
        File file2 = new File(OUT_PUT_FILE_NAME);
        try {
            if (!file1.exists()) {
                file1.createNewFile();
            }
            if (!file2.exists()) {
                file2.createNewFile();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * Io 文件拷贝
     */
    @Test
    public void test1() throws IOException {
        createFile();
        long start = System.currentTimeMillis();
        try (InputStream inputStream = new FileInputStream(FILE_NAME)) {
            OutputStream outputStream = new FileOutputStream(OUT_PUT_FILE_NAME);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            logger.info("IO file copy cost {} msc", System.currentTimeMillis() - start);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * NIO 进行文件拷贝管道方式
     */
    public void test2() {
        createFile();
        long start = System.currentTimeMillis();
        try (FileChannel inputChannel = new FileInputStream(FILE_NAME).getChannel();
             FileChannel outputChannel = new FileOutputStream(OUT_PUT_FILE_NAME).getChannel()) {
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("IO file copy cost {} msc", System.currentTimeMillis() - start);
    }

    /**
     * NIO 进行文件拷贝文件内存映射
     */
    public void test3() {
        createFile();
        long start = System.currentTimeMillis();
        try (FileInputStream fileInputStream = new FileInputStream(FILE_NAME);
             FileOutputStream fileOutputStream = new FileOutputStream(OUT_PUT_FILE_NAME)) {
            FileChannel inputStreamChannel = fileInputStream.getChannel();
            FileChannel outputStreamChannel = fileOutputStream.getChannel();
            MappedByteBuffer mappedByteBuffer = inputStreamChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputStreamChannel.size());
            outputStreamChannel.write(mappedByteBuffer);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("IO file copy cost {} msc", System.currentTimeMillis() - start);
    }


}
